<template>
<div class="box">
    

</div>
</template>

<script>
import config from "../../sgApi/config.js"

import {
    login,

} from "../../sgApi/index.js"

import VConsole from 'vconsole'
// new VConsole();
export default {
    name: "auth",
    data() {
        return {

        }
    },
    mounted() {
        // const code = this.getUrlParam('code') // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
        // console.log("code", code)
        // const local = window.location.href
        // if (code == null || code === '') {

        //     window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + config.appId + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
        // } else {
        //     // this.getOpenId(code) //把code传给后台获取用户信息
        //     localStorage.setItem("code", code)

        if(JSON.parse(localStorage.getItem("obj")).nickname =="不喝甜水"){
             new VConsole();
        }

        let data = {
            url: config.redirect_uri,
            code: localStorage.getItem("code")
        }

        login(data).then(res => {

            let obj = {
                customerCrmId: res.customerCrmId,
                openid: res.userinfo[0].openid,
                customerName: res.customerName,
                nickname: res.userinfo[0].nickname,
                avatar: res.userinfo[0].headimgurl,
                city: res.userinfo[0].city,
                province: res.userinfo[0].province,
                country: res.userinfo[0].country,
                sex: res.userinfo[0].sex,
                unionid: res.userinfo[0].unionID,
            }
            localStorage.setItem("customerCrmId", res.customerCrmId)
            localStorage.setItem("isBind", res.isCustomerBind)
            localStorage.setItem("enter_record", JSON.stringify(res.enter_record))
            localStorage.setItem("obj", JSON.stringify(obj))

        
           

         

           

            let originId = this.getUrlParam("originId") 
            if(originId == 1){


                if(res.isCustomerBind == 0){
                      this.$router.replace({
                        path: "/login"
                      })


                }else{
                       this.$router.replace({
                        path: "/giftCheck"
                     })

                }
               
             


            }else{
                if (res.isCustomerBind == 0) {
                    this.$router.replace({
                        path: "/login"
                    })
                } else {
                    this.$router.replace({
                        path: "/memberCenter"
                    })
                }
            }




        }).catch((err) => console.log(err))

        // }
    },
    methods: {
        getUrlParam(name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
            var r = window.location.search.substr(1).match(reg)
            if (r != null) return unescape(r[2])
            return null

        }
    }
}
</script>

<style lang="scss" scoped>

</style>
